﻿using System;
using System.IO;

namespace 栅格数据结构与算法
{
    class Raster
    {
        #region 构造器
        public Raster() { }
        #endregion

        #region 属性
        public int Rows { get; set; }
        public int Cols { get; set; }
        public int[,] PixelValue { get; set; }
        #endregion

        #region 方法
        public void ReadDataFromFile(string path)
        {
            try
            {
                string[] allLinesArray = File.ReadAllLines(path);
                Rows = allLinesArray.Length;
                for (int i = 0; i < Rows; i++)
                {
                    string[] lineArray = allLinesArray[i].Split(' ');
                    Cols = lineArray.Length;
                    if (PixelValue == null)
                        PixelValue = new int[Rows, Cols];
                    for (int j = 0; j < Cols; j++)
                    {
                        int value = int.Parse(lineArray[j]);
                        PixelValue[i, j] = value;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void WriteRasterMatrix()
        {
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                    Console.Write($"{PixelValue[i, j]} ");
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------");
        }

        public int GetPixelValue(int row, int col)
        {
            return PixelValue[row, col];
        }

        public void SetPixelValue(int row, int col, int pixelValue)
        {
            PixelValue[row, col] = pixelValue;
        }

        public int GetArea()
        {
            int area = 0;
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    area += PixelValue[i, j];
                }
            }
            return area;
        }

        public void ToRunLengthEncoding()
        {
            #region 变量
            RunLengthEncoding runLengthEncoding = new RunLengthEncoding();
            int i;
            int count = 0;
            int firstValue = PixelValue[0, 0];
            int lastValue = PixelValue[0, 0];
            #endregion

            #region 转换
            for (i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    lastValue = PixelValue[i, j];
                    if (firstValue != lastValue)
                    {
                        RunLengthEncodingNode runLengthEncodingNode;
                        if (j == 0)
                            runLengthEncodingNode.row = i - 1;
                        else
                            runLengthEncodingNode.row = i;
                        runLengthEncodingNode.pixelValue = firstValue;
                        runLengthEncodingNode.count = count;
                        runLengthEncoding.runLengthEncodings.Add(runLengthEncodingNode);
                        //还原
                        firstValue = lastValue;
                        count = 1;
                    }
                    else
                        count++;
                }
            }
            //插入最后一个
            RunLengthEncodingNode runLengthEncodingNode1;
            runLengthEncodingNode1.row = i - 1;
            runLengthEncodingNode1.pixelValue = firstValue;
            runLengthEncodingNode1.count = count;
            runLengthEncoding.runLengthEncodings.Add(runLengthEncodingNode1);
            #endregion

            //打印
            runLengthEncoding.WriteRunLengthEncoding();
        }
        #endregion
    }
}
